home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / textutl3.lha / textutils-1.3 / src / head.c < prev    next >
C/C++ Source or Header  |  1992-06-29  |  8KB  |  381 lines

  1. /* head -- output first part of file(s)
  2.    Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Options:
  19.    -b            Print first N 512-byte blocks.
  20.    -c, --bytes=N[bkm]    Print first N bytes
  21.             [or 512-byte blocks, kilobytes, or megabytes].
  22.    -k            Print first N kilobytes.
  23.    -N, -l, -n, --lines=N    Print first N lines.
  24.    -m            Print first N megabytes.
  25.    -q, --quiet, --silent    Never print filename headers.
  26.    -v, --verbose        Always print filename headers.
  27.  
  28.    Reads from standard input if no files are given or when a filename of
  29.    ``-'' is encountered.
  30.    By default, filename headers are printed only if more than one file
  31.    is given.
  32.    By default, prints the first 10 lines (head -n 10).
  33.  
  34.    David MacKenzie <djm@ai.mit.edu> */
  35.  
  36. #include <stdio.h>
  37. #include <getopt.h>
  38. #include <ctype.h>
  39. #include <sys/types.h>
  40. #include "system.h"
  41.  
  42. #ifdef isascii
  43. #define ISDIGIT(c) (isascii ((c)) && isdigit ((c)))
  44. #else
  45. #define ISDIGIT(c) (isdigit ((c)))
  46. #endif
  47.  
  48. /* Number of lines/chars/blocks to head. */
  49. #define DEFAULT_NUMBER 10
  50.  
  51. /* Size of atomic reads. */
  52. #define BUFSIZE (512 * 8)
  53.  
  54. /* Number of bytes per item we are printing.
  55.    If 0, head in lines. */
  56. int unit_size;
  57.  
  58. /* If nonzero, print filename headers. */
  59. int print_headers;
  60.  
  61. /* When to print the filename banners. */
  62. enum header_mode
  63. {
  64.   multiple_files, always, never
  65. };
  66.  
  67. int head ();
  68. int head_bytes ();
  69. int head_file ();
  70. int head_lines ();
  71. long atou ();
  72. void error ();
  73. void parse_unit ();
  74. void usage ();
  75. void write_header ();
  76. void xwrite ();
  77.  
  78. /* The name this program was run with. */
  79. char *program_name;
  80.  
  81. /* Have we ever read standard input?  */
  82. int have_read_stdin;
  83.  
  84. struct option long_options[] =
  85. {
  86.   {"bytes", 1, NULL, 'c'},
  87.   {"lines", 1, NULL, 'n'},
  88.   {"quiet", 0, NULL, 'q'},
  89.   {"silent", 0, NULL, 'q'},
  90.   {"verbose", 0, NULL, 'v'},
  91.   {NULL, 0, NULL, 0}
  92. };
  93.  
  94. void
  95. main (argc, argv)
  96.      int argc;
  97.      char **argv;
  98. {
  99.   enum header_mode header_mode = multiple_files;
  100.   int exit_status = 0;
  101.   long number = -1;        /* Number of items to print (-1 if undef.). */
  102.   int c;            /* Option character. */
  103.  
  104.   program_name = argv[0];
  105.   have_read_stdin = 0;
  106.   unit_size = 0;
  107.   print_headers = 0;
  108.  
  109.   if (argc > 1 && argv[1][0] == '-' && ISDIGIT (argv[1][1]))
  110.     {
  111.       /* Old option syntax; a dash, one or more digits, and one or
  112.      more option letters.  Move past the number. */
  113.       for (number = 0, ++argv[1]; ISDIGIT (*argv[1]); ++argv[1])
  114.     number = number * 10 + *argv[1] - '0';
  115.       /* Parse any appended option letters. */
  116.       while (*argv[1])
  117.     {
  118.       switch (*argv[1])
  119.         {
  120.         case 'b':
  121.           unit_size = 512;
  122.           break;
  123.  
  124.         case 'c':
  125.           unit_size = 1;
  126.           break;
  127.  
  128.         case 'k':
  129.           unit_size = 1024;
  130.           break;
  131.  
  132.         case 'l':
  133.           unit_size = 0;
  134.           break;
  135.  
  136.         case 'm':
  137.           unit_size = 1048576;
  138.           break;
  139.  
  140.         case 'q':
  141.           header_mode = never;
  142.           break;
  143.  
  144.         case 'v':
  145.           header_mode = always;
  146.           break;
  147.  
  148.         default:
  149.           error (0, 0, "unrecognized option `-%c'", *argv[1]);
  150.           usage ();
  151.         }
  152.       ++argv[1];
  153.     }
  154.       /* Make the options we just parsed invisible to getopt. */
  155.       argv[1] = argv[0];
  156.       argv++;
  157.       argc--;
  158.     }
  159.  
  160.   while ((c = getopt_long (argc, argv, "c:n:qv", long_options, (int *) 0))
  161.      != EOF)
  162.     {
  163.       switch (c)
  164.     {
  165.     case 'c':
  166.       unit_size = 1;
  167.       parse_unit (optarg);
  168.       goto getnum;
  169.     case 'n':
  170.       unit_size = 0;
  171.     getnum:
  172.       number = atou (optarg);
  173.       if (number == -1)
  174.         error (1, 0, "invalid number `%s'", optarg);
  175.       break;
  176.  
  177.     case 'q':
  178.       header_mode = never;
  179.       break;
  180.  
  181.     case 'v':
  182.       header_mode = always;
  183.       break;
  184.  
  185.     default:
  186.       usage ();
  187.     }
  188.     }
  189.  
  190.   if (number == -1)
  191.     number = DEFAULT_NUMBER;
  192.  
  193.   if (unit_size > 1)
  194.     number *= unit_size;
  195.  
  196.   if (header_mode == always
  197.       || (header_mode == multiple_files && optind < argc - 1))
  198.     print_headers = 1;
  199.  
  200.   if (optind == argc)
  201.     exit_status |= head_file ("-", number);
  202.  
  203.   for (; optind < argc; ++optind)
  204.     exit_status |= head_file (argv[optind], number);
  205.  
  206.   if (have_read_stdin && close (0) < 0)
  207.     error (1, errno, "-");
  208.   if (close (1) < 0)
  209.     error (1, errno, "write error");
  210.  
  211.   exit (exit_status);
  212. }
  213.  
  214. int
  215. head_file (filename, number)
  216.      char *filename;
  217.      long number;
  218. {
  219.   int fd;
  220.  
  221.   if (!strcmp (filename, "-"))
  222.     {
  223.       have_read_stdin = 1;
  224.       filename = "standard input";
  225.       if (print_headers)
  226.     write_header (filename);
  227.       return head (filename, 0, number);
  228.     }
  229.   else
  230.     {
  231.       fd = open (filename, O_RDONLY);
  232.       if (fd >= 0)
  233.     {
  234.       int errors;
  235.  
  236.       if (print_headers)
  237.         write_header (filename);
  238.       errors = head (filename, fd, number);
  239.       if (close (fd) == 0)
  240.         return errors;
  241.     }
  242.       error (0, errno, "%s", filename);
  243.       return 1;
  244.     }
  245. }
  246.  
  247. void
  248. write_header (filename)
  249.      char *filename;
  250. {
  251.   static int first_file = 1;
  252.  
  253.   if (first_file)
  254.     {
  255.       xwrite (1, "==> ", 4);
  256.       first_file = 0;
  257.     }
  258.   else
  259.     xwrite (1, "\n==> ", 5);
  260.   xwrite (1, filename, strlen (filename));
  261.   xwrite (1, " <==\n", 5);
  262. }
  263.  
  264. int
  265. head (filename, fd, number)
  266.      char *filename;
  267.      int fd;
  268.      long number;
  269. {
  270.   if (unit_size)
  271.     return head_bytes (filename, fd, number);
  272.   else
  273.     return head_lines (filename, fd, number);
  274. }
  275.  
  276. int
  277. head_bytes (filename, fd, bytes_to_write)
  278.      char *filename;
  279.      int fd;
  280.      long bytes_to_write;
  281. {
  282.   char buffer[BUFSIZE];
  283.   int bytes_read;
  284.  
  285.   while (bytes_to_write)
  286.     {
  287.       bytes_read = read (fd, buffer, BUFSIZE);
  288.       if (bytes_read == -1)
  289.     {
  290.       error (0, errno, "%s", filename);
  291.       return 1;
  292.     }
  293.       if (bytes_read == 0)
  294.     break;
  295.       if (bytes_read > bytes_to_write)
  296.     bytes_read = bytes_to_write;
  297.       xwrite (1, buffer, bytes_read);
  298.       bytes_to_write -= bytes_read;
  299.     }
  300.   return 0;
  301. }
  302.  
  303. int
  304. head_lines (filename, fd, lines_to_write)
  305.      char *filename;
  306.      int fd;
  307.      long lines_to_write;
  308. {
  309.   char buffer[BUFSIZE];
  310.   int bytes_read;
  311.   int bytes_to_write;
  312.  
  313.   while (lines_to_write)
  314.     {
  315.       bytes_read = read (fd, buffer, BUFSIZE);
  316.       if (bytes_read == -1)
  317.     {
  318.       error (0, errno, "%s", filename);
  319.       return 1;
  320.     }
  321.       if (bytes_read == 0)
  322.     break;
  323.       bytes_to_write = 0;
  324.       while (bytes_to_write < bytes_read)
  325.     if (buffer[bytes_to_write++] == '\n' && --lines_to_write == 0)
  326.       break;
  327.       xwrite (1, buffer, bytes_to_write);
  328.     }
  329.   return 0;
  330. }
  331.  
  332. void
  333. parse_unit (str)
  334.      char *str;
  335. {
  336.   int arglen = strlen (str);
  337.  
  338.   if (arglen == 0)
  339.     return;
  340.  
  341.   switch (str[arglen - 1])
  342.     {
  343.     case 'b':
  344.       unit_size = 512;
  345.       str[arglen - 1] = '\0';
  346.       break;
  347.     case 'k':
  348.       unit_size = 1024;
  349.       str[arglen - 1] = '\0';
  350.       break;
  351.     case 'm':
  352.       unit_size = 1048576;
  353.       str[arglen - 1] = '\0';
  354.       break;
  355.     }
  356. }
  357.  
  358. /* Convert STR, a string of ASCII digits, into an unsigned integer.
  359.    Return -1 if STR does not represent a valid unsigned integer. */
  360.  
  361. long
  362. atou (str)
  363.      char *str;
  364. {
  365.   int value;
  366.  
  367.   for (value = 0; ISDIGIT (*str); ++str)
  368.     value = value * 10 + *str - '0';
  369.   return *str ? -1 : value;
  370. }
  371.  
  372. void
  373. usage ()
  374. {
  375.   fprintf (stderr, "\
  376. Usage: %s [-c N[bkm]] [-n N] [-qv] [--bytes=N[bkm]] [--lines=N]\n\
  377.        [--quiet] [--silent] [--verbose] [file...]\n\
  378.        %s [-Nbcklmqv] [file...]\n", program_name, program_name);
  379.   exit (1);
  380. }
  381.